// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/SingleQuestWidget.h"
#include "Components/Border.h"
#include "Components/TextBlock.h"
#include "Components/SizeBox.h"
#include "Components/Button.h"
// #include "Libraries/CommonLibrary.h"

void USingleQuestWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	DefaultBGColor = FLinearColor(0.007843f, 0.011765f, 0.019608f, 0.298039f);
	HoveredBGColor = FLinearColor(0.039216f, 0.062745f, 0.101961f, 0.498039f);
	SDefaultBGColor = FLinearColor(0.101961f, 0.090196f, 0.043137f, 0.498039f);
	SHoveredBGColor = FLinearColor(0.070588f, 0.062745f, 0.027451f, 0.498039f);
	DefaultFrameColor = FLinearColor(0.129412f, 0.164706f, 0.239216f, 0.298039f);
	HoveredFrameColor = FLinearColor(0.2f, 0.282353f, 0.458824f, 0.298039f);
	SDefaultFrameColor = FLinearColor(0.101961f, 0.090196f, 0.043137f, 0.498039f);
	SHoveredFrameColor = FLinearColor(0.070588f, 0.062745f, 0.027451f, 0.498039f);
	DefaultSuggestedColor = FLinearColor(0.047059f, 0.047059f, 0.070588f, 1.f);
	HoveredSuggestedColor = FLinearColor(0.082353f, 0.082353f, 0.145098f, 1.f);
	SDefaultSuggestedColor = FLinearColor(0.168627f, 0.14902f, 0.070588f, 1.f);
	SHoveredSuggestedColor = FLinearColor(0.168627f, 0.14902f, 0.070588f, 1.f);
	FScriptDelegate Click;
	Click.BindUFunction(this, "ClickEvent");
	FScriptDelegate Hovered;
	Hovered.BindUFunction(this, "HoveredEvent");
	FScriptDelegate UnHovered;
	UnHovered.BindUFunction(this, "UnHoveredEvent");
	DummyButton->OnClicked.Add(Click);
	DummyButton->OnHovered.Add(Hovered);
	DummyButton->OnUnhovered.Add(UnHovered);
}

void USingleQuestWidget::ConstructWidget(const FMissionMaster& LMissionInfo) {
	MissionInfo = LMissionInfo;
}

void USingleQuestWidget::NativePreConstruct() {
	Super::NativePreConstruct();
	MissionTitle->SetText(MissionInfo.MissionMainInfo.Name);
	RecommendedLevel->SetText(FText::FromString(FString::FromInt(MissionInfo.MissionMainInfo.SuggestedLevel)));
	const UEnum* Region = StaticEnum<ERegion>();
	int32 Index = Region->GetIndexByValue((int64)MissionInfo.MissionMainInfo.Region);
	MissionSubtitle->SetText(Region->GetDisplayNameTextByIndex(Index));
}

void USingleQuestWidget::SetQuestActive() {
	Sizer->SetHeightOverride(100.f);
	BG->SetBrushColor(SDefaultBGColor);
	Frame->SetBrushColor(SDefaultFrameColor);
	SuggestedBorder->SetBrushColor(SDefaultSuggestedColor);
	MissionTitle->SetColorAndOpacity(FSlateColor(FLinearColor(1.f, 1.f, 1.f)));
	IsActiveQuest = true;
}

void USingleQuestWidget::SetQuestInActive() {
	Sizer->SetHeightOverride(70.f);
	BG->SetBrushColor(DefaultBGColor);
	Frame->SetBrushColor(DefaultFrameColor);
	SuggestedBorder->SetBrushColor(DefaultSuggestedColor);
	MissionTitle->SetColorAndOpacity(FSlateColor(FLinearColor(0.215686f, 0.215686f, 0.215686f)));
	IsActiveQuest = false;
}

void USingleQuestWidget::SetSelected() {
	OnHovered();
	IsSelected = true;
}

void USingleQuestWidget::SetUnSelected() {
	OnUnHovered();
	IsSelected = false;
}

void USingleQuestWidget::OnHovered() {
	BG->SetBrushColor(IsActiveQuest ? SHoveredBGColor : HoveredBGColor);
	Frame->SetBrushColor(IsActiveQuest ? SDefaultFrameColor : DefaultFrameColor);
	SuggestedBorder->SetBrushColor(IsActiveQuest ? SDefaultSuggestedColor : DefaultSuggestedColor);
	MissionTitle->SetColorAndOpacity(FSlateColor(FLinearColor(1.f, 1.f, 1.f)));
}

void USingleQuestWidget::OnUnHovered() {
	BG->SetBrushColor(IsActiveQuest ? SHoveredBGColor : HoveredBGColor);
	Frame->SetBrushColor(IsActiveQuest ? SDefaultFrameColor : DefaultFrameColor);
	SuggestedBorder->SetBrushColor(IsActiveQuest ? SDefaultSuggestedColor : DefaultSuggestedColor);
	MissionTitle->SetColorAndOpacity(IsActiveQuest ? FSlateColor(FLinearColor(1.f, 1.f, 1.f)) : FSlateColor(FLinearColor(0.215686f, 0.215686f, 0.215686f)));
}

void USingleQuestWidget::ClickEvent() {
	OnHovered();
	// DEBUG_MESSAGE("ClickEvent");
	ClickedToQuest.Broadcast(this);
}

void USingleQuestWidget::HoveredEvent() {
	OnHovered();
	// DEBUG_MESSAGE("HoveredEvent");
	HoveredToQuest.Broadcast(this);
}

void USingleQuestWidget::UnHoveredEvent() {
	OnUnHovered();
	// DEBUG_MESSAGE("UnHoveredEvent");
	if (!IsSelected) {
		UnHoveredQuest.Broadcast();
	}
}

void USingleQuestWidget::SimulateClick() {
	ClickEvent();
}